package cn.iocoder.an.module.business.dal.mysql.repairrequest;

import java.util.*;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.an.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.an.module.business.dal.dataobject.repairrequest.RepairRequestDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.micrometer.common.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.an.module.business.controller.admin.repairrequest.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 维修申请 Mapper
 *
 * @author zzy
 */
@Mapper
public interface RepairRequestMapper extends BaseMapperX<RepairRequestDO> {

    default PageResult<RepairRequestDO> selectPage(RepairRequestPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RepairRequestDO>()
                .eqIfPresent(RepairRequestDO::getFaultId, reqVO.getFaultId())
                .likeIfPresent(RepairRequestDO::getEquipmentName, reqVO.getEquipmentName())
                .eqIfPresent(RepairRequestDO::getUrgency, reqVO.getUrgency())
                .eqIfPresent(RepairRequestDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(RepairRequestDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RepairRequestDO::getUpdateTime)
                .orderByDesc(RepairRequestDO::getStatus));

    }

    default PageResult<RepairRequestDO> selectTaskPage(RepairRequestPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RepairRequestDO>()
                .eqIfPresent(RepairRequestDO::getFaultId, reqVO.getFaultId())
                .likeIfPresent(RepairRequestDO::getEquipmentName, reqVO.getEquipmentName())
                .eqIfPresent(RepairRequestDO::getUrgency, reqVO.getUrgency())
                .eqIfPresent(RepairRequestDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(RepairRequestDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(RepairRequestDO::getId,reqVO.getIds())
                .orderByDesc(RepairRequestDO::getUpdateTime)
                .orderByDesc(RepairRequestDO::getStatus));
    }


    default PageResult<RepairRequestDO> selectLatestByEquipmentId1(RepairRequestPageReqVO reqVO) {
        MPJLambdaWrapper<RepairRequestDO> wrapper = new MPJLambdaWrapper<RepairRequestDO>()
                .select(RepairRequestDO::getId, RepairRequestDO::getRepairNo, RepairRequestDO::getEquipmentId)
                .eqIfExists(RepairRequestDO::getFaultId, reqVO.getFaultId())
                .likeIfExists(RepairRequestDO::getEquipmentName, reqVO.getEquipmentName())
                .eqIfExists(RepairRequestDO::getUrgency, reqVO.getUrgency())
                .eqIfExists(RepairRequestDO::getStatus, reqVO.getStatus())
                .apply("ROW_NUMBER() OVER (PARTITION BY t.equipment_id ORDER BY t.update_time DESC) AS row_num")
                .apply("WHERE row_num = 1");

        // 分页查询
        return selectPage(reqVO, wrapper);
    }

    default PageResult<RepairRequestDO> selectLatestByEquipmentId(RepairRequestPageReqVO reqVO) {
        MPJLambdaWrapper<RepairRequestDO> wrapper = new MPJLambdaWrapper<RepairRequestDO>()
                .select( RepairRequestDO::getId,
                        RepairRequestDO::getRepairNo,
                        RepairRequestDO::getEquipmentId,
                        RepairRequestDO::getFaultId,
                        RepairRequestDO::getEquipmentName,
                        RepairRequestDO::getFaultTime,
                        RepairRequestDO::getRequestUserId,
                        RepairRequestDO::getRequestUserName,
                        RepairRequestDO::getUrgency,
                        RepairRequestDO::getDescription,
                        RepairRequestDO::getRepairWay,
                        RepairRequestDO::getDutyUserId,
                        RepairRequestDO::getDutyUserName,
                        RepairRequestDO::getStatus,
                        RepairRequestDO::getResolved,
                        RepairRequestDO::getExpectedResolveTime,
                        RepairRequestDO::getCreator,
                        RepairRequestDO::getCreateTime,
                        RepairRequestDO::getUpdater,
                        RepairRequestDO::getUpdateTime
                        )
                .eqIfExists(RepairRequestDO::getFaultId, reqVO.getFaultId())
                .likeIfExists(RepairRequestDO::getEquipmentName, reqVO.getEquipmentName())
                .eqIfExists(RepairRequestDO::getUrgency, reqVO.getUrgency())
                .eqIfExists(RepairRequestDO::getStatus, reqVO.getStatus())
                .apply(" t.update_time = (" +
                        "    SELECT MAX(r.update_time) " +
                        "    FROM business_repair_request r " +
                        "    WHERE r.equipment_id = t.equipment_id" +
                        "    AND r.tenant_id = t.tenant_id" +
                        ")");

        // 分页查询
        return selectPage(reqVO, wrapper);
    }






    @Select("SELECT MAX(repair_no) FROM business_repair_request WHERE repair_no LIKE CONCAT(#{prefix}, '%')")
    String findMaxRepairtNo(@Param("prefix") String prefix);

}